package com.company.zoj;

import java.util.*;

/**
 * Created by v on 17-4-25.
 */
public class ZojTest1805 {
    private static int n;
    private static int m;
    private static int[][] map;
    private static Node[] color=new Node[2];
    private static Node root=new Node(4,0);
    private static int number;
    private static int trees;
    private static HashMap<Node,Node> set;

    public static void main(String[] args) {
        color[0]=new Node();
        color[1]=new Node();

        Scanner input=new Scanner(System.in);
        n=input.nextInt();
        m=input.nextInt();
        while (n!=0&&m!=0){
            number=1;
            trees=0;
            set=new HashMap<>();
            map=new int[128][128];
            String[] strings=new String[n];
            for(int i=0;i<n;i++){
                String in=input.next();
                for(int j=0;j<in.length();j++){
                    map[i][j]=(in.charAt(j)-'0')%10;
                }
            }
            //
            n=power(n);
            m=power(m);
            n=Math.max(n,m)-1;
            m=0;
            init();
            System.out.println(number+" "+trees);
            n=input.nextInt();
            m=input.nextInt();
        }
    }

    //
    private static int power(int number){
        number=number-1;
        int power=1;
        while (number!=0){
            power*=2;
            number/=2;
        }
        return power;
    }

    private static class Index{
        int x1;
        int y1;
        int x2;
        int y2;

        public Index(int x1,int y1,int x2,int y2){
            this.x1=x1;
            this.y1=y1;
            this.x2=x2;
            this.y2=y2;
        }

        public Index quadrant(int quadrant){
            Index index=new Index(x1,y1,x2,y2);

            switch (quadrant){
                case 0:{
                    index.x2=(x1+x2)/2;
                    index.y2=(y1+y2)/2;
                    break;
                }
                case 1:{
                    index.x1=(x1+x2)/2+1;
                    index.y2=(y1+y2)/2;
                    break;
                }
                case 2:{
                    index.y1=(y1+y2)/2+1;
                    index.x2=(x1+x2)/2;
                    break;
                }
                case 3:{
                    index.x1=(x1+x2)/2+1;
                    index.y1=(y1+y2)/2+1;
                    break;
                }
            }

            return index;
        }
    }

    private static void init(){
        root=structure(new Index(0,0,n,n),1);
        if(root.children==0){
            trees=1;
            return;
        }

        //
        trees=set.size();

        //
        Iterator<Node> iterator=set.keySet().iterator();
        while (iterator.hasNext()){
            Node node=iterator.next();
            for(int i=0;i<node.nodes.length;i++){
                if(node.nodes[i].children==0){
                    trees++;
                }
            }
        }
    }

    //
    private static Node structure(Index index,int floor){
        if(index.x1==index.x2){
            return color[map[index.y1][index.x1]];
        }

        Node current=new Node(4,floor);
        for(int i=0;i<current.nodes.length;i++){
            current.nodes[i]=structure(index.quadrant(i),floor+1);
            number++;
        }

        if(current.allEquals()){
            number=number-4;
            current=current.nodes[0];
            return current;
        }else if(set.containsKey(current)){
            return set.get(current);
        }else {
            set.put(current,current);
            return current;
        }
    }

    //
    private static class Node{
        //
        int children;
        //
        Node[] nodes;
        //
        int floor=-1;

        public Node(int children,int floor){
            this.floor=floor;
            this.children=children;
            nodes=new Node[4];
        }

        public Node(){
            children=0;
            nodes=new Node[0];
        }

        @Override
        public boolean equals(Object obj) {
            Node node=(Node)obj;

            if(node.children==0||this.children==0){
                return false;
            }

            for(int i=0;i<node.children;i++){
                if(this.nodes[i]!=node.nodes[i]){
                    return false;
                }
            }

            return true;
        }

        @Override
        public int hashCode() {
            return floor;
        }

        //
        public boolean allEquals(){
            if(children==0){
                return false;
            }

            if(nodes[0].children!=0){
                return false;
            }

            return nodes[0]==nodes[1]
                    &&nodes[1]==nodes[2]
                    &&nodes[2]==nodes[3];
        }
    }

    private static boolean same(String[] strings){
        for (int i=1;i<strings.length;i++){
            if(!strings[i].equals(strings[i-1])){
                return false;
            }
        }
        return true;
    }
}